﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;

namespace KinectClient
{
    class Program
    {
        private static byte[] buffer = new byte[6000];

        static void Main(string[] args)
        {
            while (true)
            {
                using (TcpClient client = new TcpClient())
                {
                    client.ReceiveTimeout = 999999;
                    try
                    {
                        client.Connect(KinectServer.Server.serverEP.Address, KinectServer.Server.serverEP.Port);
                        while (client.Connected)
                        {
                            try
                            {
                                using (NetworkStream stream = client.GetStream())
                                {
                                    if (stream.CanRead)
                                    {
                                        byte[] bufferLength = new byte[4];
                                        try
                                        {
                                            stream.Read(bufferLength, 0, 4);
                                        }
                                        catch (Exception e) { Console.Error.WriteLine("*Failed to read size of buffer.\n{0}", e.Message); }

                                        int length = BitConverter.ToInt32(bufferLength, 0) - 4;
                                        int count = 0;
                                        try
                                        {
                                            stream.Read(buffer, count, length);
                                        }
                                        catch (Exception e) { Console.Error.WriteLine("*Failed to read buffer with size {1}.\n{0}", e.Message, length); }

                                        try
                                        {
                                            Console.WriteLine("*Frame Number: {0}", BitConverter.ToInt64(buffer, count)); count += KinectServer.Server.INT64_BYTES;

                                            Console.WriteLine("*TimeStamp: {0}", BitConverter.ToInt64(buffer, count)); count += KinectServer.Server.INT64_BYTES;

                                            if (length > 16) // has joint info
                                            {
                                                for (int joint = 0; joint <= 19; joint ++)
                                                {
                                                    Console.WriteLine("*JOINT {0}", joint);

                                                    Console.WriteLine("*Joint1.TX: {0}", BitConverter.ToDouble(buffer, count)); count += KinectServer.Server.DOUBLE_BYTES;
                                                    Console.WriteLine("*Joint1.TY: {0}", BitConverter.ToDouble(buffer, count)); count += KinectServer.Server.DOUBLE_BYTES;
                                                    Console.WriteLine("*Joint1.TZ: {0}", BitConverter.ToDouble(buffer, count)); count += KinectServer.Server.DOUBLE_BYTES;

                                                    //Console.WriteLine("*Joint1.RX: {0}", BitConverter.ToDouble(buffer, count)); count += KinectServer.Server.DOUBLE_BYTES;
                                                    //Console.WriteLine("*Joint1.RY: {0}", BitConverter.ToDouble(buffer, count)); count += KinectServer.Server.DOUBLE_BYTES;
                                                    //Console.WriteLine("*Joint1.RZ: {0}", BitConverter.ToDouble(buffer, count)); count += KinectServer.Server.DOUBLE_BYTES;
                                                }
                                            }
                                        }
                                        catch (Exception e) { Console.Error.WriteLine("*Error accessing buffer.\n{0}", e.Message); }

                                        Console.WriteLine();
                                    }
                                    else
                                    {
                                        Console.WriteLine("*Can't read");
                                    }
                                }
                                Thread.Sleep(KinectServer.Program.mspf);
                            }
                            catch (Exception e) { Console.Error.WriteLine("*Failed to receive message.\n{0}", e.Message); }
                        }
                    }
                    catch (Exception e) { Console.Error.WriteLine("*Error Connecting.\n{0}", e.Message); }
                }
            }
        }
    }
}
